<?php

namespace app\api\model\wanlshop;

use think\Model;
use fast\Random;

class Shop extends Model
{

    // 表名
    protected $name = 'wanlshop_shop';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'isself', //追加是否为自营店
        'find_user',
        'created_at'
    ];

    public function getIsselfAttr($value, $data)
    {
        return isset($data['isself']) ? $data['isself'] : '';
    }

    public function getCreatedAtAttr()
    {
        return date('Y-m-d H:i:s', $this->createtime);
    }

    // 获取店铺 发现号
    public function getFindUserAttr($value, $data)
    {
        $find = [];
        if (isset($data['user_id'])) {
            $findModel = new find\User;
            $row = $findModel
                ->where(['user_id' => $data['user_id']])
                ->find();
            if (!$row) {
                $findModel->user_id = $data['user_id'];
                $findModel->user_no = Random::nozero(9);
                $findModel->save();
                $find = [
                    'user_no' => $findModel->user_no,
                    'fans' => 0
                ];
            } else {
                $find = [
                    'user_no' => $row->user_no,
                    'fans' => $row->fans
                ];
            }
        }
        return $find;
    }

    public function getServiceIdsAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['service_ids']) ? $data['service_ids'] : '');
        $valueArr = explode(',', $value);
        $service = [];
        foreach (ShopService::all($valueArr) as $vo) {
            $service[] = [
                'id' => $vo['id'],
                'name' => $vo['name'],
                'description' => $vo['description']
            ];
        }
        return $service;
    }
}
